const Job = require('../models/jobModel');

// Get nearby jobs based on coordinates
exports.getNearbyJobs = async (req, res) => {
  try {
    const { lat, lng } = req.query;
    
    // Validate coordinates
    if (!lat || !lng) {
      return res.status(400).json({
        status: 'fail',
        message: 'Please provide latitude and longitude'
      });
    }
    
    // Convert string parameters to numbers
    const latitude = parseFloat(lat);
    const longitude = parseFloat(lng);
    
    // Default distance is 10km (distance in meters)
    const distance = req.query.distance ? parseFloat(req.query.distance) * 1000 : 10000;
    
    const jobs = await Job.find({
      location: {
        $near: {
          $geometry: {
            type: 'Point',
            coordinates: [longitude, latitude]
          },
          $maxDistance: distance
        }
      },
      isActive: true
    }).limit(50);
    
    res.status(200).json({
      status: 'success',
      results: jobs.length,
      data: {
        jobs
      }
    });
  } catch (err) {
    res.status(400).json({
      status: 'fail',
      message: err.message
    });
  }
};

// Get latest jobs
exports.getLatestJobs = async (req, res) => {
  try {
    // Pagination
    const page = req.query.page * 1 || 1;
    const limit = req.query.limit * 1 || 20;
    const skip = (page - 1) * limit;
    
    const jobs = await Job.find({ isActive: true })
      .sort({ postedDate: -1 })
      .skip(skip)
      .limit(limit);
    
    res.status(200).json({
      status: 'success',
      results: jobs.length,
      data: {
        jobs
      }
    });
  } catch (err) {
    res.status(400).json({
      status: 'fail',
      message: err.message
    });
  }
};

// Search jobs by keyword
exports.searchJobs = async (req, res) => {
  try {
    const { keyword } = req.query;
    
    if (!keyword) {
      return res.status(400).json({
        status: 'fail',
        message: 'Please provide a search keyword'
      });
    }
    
    // Pagination
    const page = req.query.page * 1 || 1;
    const limit = req.query.limit * 1 || 20;
    const skip = (page - 1) * limit;
    
    const jobs = await Job.find(
      {
        $text: { $search: keyword },
        isActive: true
      },
      { 
        score: { $meta: 'textScore' } 
      }
    )
      .sort({ score: { $meta: 'textScore' } })
      .skip(skip)
      .limit(limit);
    
    res.status(200).json({
      status: 'success',
      results: jobs.length,
      data: {
        jobs
      }
    });
  } catch (err) {
    res.status(400).json({
      status: 'fail',
      message: err.message
    });
  }
};

// Get job by ID
exports.getJob = async (req, res) => {
  try {
    const job = await Job.findById(req.params.id);
    
    if (!job) {
      return res.status(404).json({
        status: 'fail',
        message: 'No job found with that ID'
      });
    }
    
    res.status(200).json({
      status: 'success',
      data: {
        job
      }
    });
  } catch (err) {
    res.status(400).json({
      status: 'fail',
      message: err.message
    });
  }
}; 